package com.foocus.foocuscloud.common.aop.log;

import com.foocus.foocuscloud.common.api.dto.LogRecordDTO;
import com.foocus.foocuscloud.common.api.rpc.RpcRestService;
import com.foocus.foocuscloud.common.domain.ResponseWrapper;
import com.foocus.foocuscloud.common.utils.UUIDUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Aspect
@Component
@Order(1)
public class LogWriteWrapper {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogWriteWrapper.class);

/*    @Autowired
    private RpcRestService rpcRestService;*/

    @Resource
    private PlatformTransactionManager transactionManager;

    @Pointcut(value = "@annotation(LogWrite)")
    private void pointCut() {

    }

    @Before(value = "pointCut()")
    public void logWrite(JoinPoint point) {

        handleLog(point);
    }

    private void handleLog(JoinPoint point) {

        LOGGER.info(">>>>>>>>LogWrite Aspect Wrapper Start.<<<<<<<<");
        MethodSignature signature = (MethodSignature) point.getSignature();
        Class returnType = signature.getReturnType();
        LOGGER.info("ReturnType is {}", returnType);

        Method method = signature.getMethod();
        LogWrite annotation = method.getAnnotation(LogWrite.class);
        String value = annotation.value();
        LOGGER.info("LogWrite Annotation Value is {}", value);

        Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
        for (Annotation declaredAnnotation : declaredAnnotations) {
            if (declaredAnnotation instanceof GetMapping) {
                LOGGER.info("GetMapping Annotation Value is {}", declaredAnnotation);
            } else if (declaredAnnotation instanceof PostMapping) {
                LOGGER.info("PostMapping Annotation Value is {}", declaredAnnotation);
            }
            LOGGER.info("declaredAnnotation is {}", declaredAnnotation.annotationType().getName());
        }
        /*LogRecordDTO logRecord = new LogRecordDTO();
        logRecord.setId(UUIDUtil.uuid());
        logRecord.setContent(value);
        logRecord.setOperateType("INSERT");
        logRecord.setUserId("000000");
        logRecord.setUserName("codemine");
        rpcRestService.addLogWrapper(logRecord);
        ResponseWrapper logWrapper2 = rpcRestService.getLogWrapper2("1");
        LOGGER.info("日志RPC: {}", logWrapper2.toString());*/
        LOGGER.info(">>>>>>>>LogWrite Aspect Wrapper End.<<<<<<<<");
    }
}
